package com.bjyhl.medical.main.video;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjyhl.core.fragments.LatteFragment;
import com.bjyhl.core.fragments.bottom.BaseBottomFragment;
import com.bjyhl.core.net.RestClient;
import com.bjyhl.core.net.callback.IError;
import com.bjyhl.core.net.callback.IFailure;
import com.bjyhl.core.net.callback.ISuccess;
import com.bjyhl.medical.R;
import com.bjyhl.ui.recycler.ItemType;
import com.bjyhl.ui.recycler.MultipleItemEntity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.joanzapata.iconify.widget.IconTextView;

import java.util.List;

/**
 * Created by Natalya on 2019/1/27 18:22
 * Note: 主页fragment
 */

public class VideoCategoryFragment extends LatteFragment implements View.OnClickListener {
    TextView tv_category_title = null;
    TextView tv_category_tostar = null;
    TextView tv_category_director = null;
    TextView tv_category_introduction = null;
    ImageView iv_category = null;
    IconTextView icon_back = null;
    public static final String ARG_VIDEO_CATEGORY = "ARG_VIDEO_CATEGORY";
    public static final String ARG_VIDEO_CATEGORY_NAME = "ARG_VIDEO_CATEGORY_NAME";
    private Integer categoryId = 0;
    private String categoryName = null;
    private BaseBottomFragment baseBottomFragment;
    private static final RequestOptions REQUEST_OPTIONS =
            new RequestOptions()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .dontAnimate();

    public static VideoCategoryFragment newInstance(String categoryName, Integer categoryId, BaseBottomFragment baseBottomFragment) {
        final Bundle args = new Bundle();
        args.putInt(ARG_VIDEO_CATEGORY, categoryId);
        args.putString(ARG_VIDEO_CATEGORY_NAME, categoryName);
        final VideoCategoryFragment fragment = new VideoCategoryFragment();
        fragment.baseBottomFragment = baseBottomFragment;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        categoryId = args.getInt(ARG_VIDEO_CATEGORY);
        categoryName = args.getString(ARG_VIDEO_CATEGORY_NAME);
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_videos_category;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View root) {
        tv_category_title = $(R.id.tv_category_title);
        tv_category_tostar = $(R.id.tv_category_tostar);
        tv_category_director = $(R.id.tv_category_director);
        tv_category_introduction = $(R.id.tv_category_introduction);
        iv_category = $(R.id.iv_category);
        icon_back = $(R.id.icon_back);
        icon_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        pop();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        // http://192.168.8.101:3000/v1/movie/getCategory/38
        // http://192.168.8.101:3000/v1/movie/getMoviesByCategory/38
//        VideoCategoryFragment videoOneTypeFragment = VideoCategoryFragment.newInstance(mVideoType, 9999, baseBottomFragment);
//        baseBottomFragment.getSupportDelegate().start(videoOneTypeFragment);

//        getSupportDelegate().loadRootFragment(R.id.movie_title_container, VideoTitleFragment.newInstance(mVideoType));
//        getSupportDelegate().loadRootFragment(R.id.movie_container, VideoBlockFragment.newInstance(mVideoType, mVideoCount, baseBottomFragment));
        getSupportDelegate().loadRootFragment(R.id.movie_container, VideoCategoryBlockFragment.newInstance(categoryName, categoryId, baseBottomFragment));
        RestClient.builder()
                .url("http://192.168.8.101:3000/v1/movie/getCategory/"+categoryId)
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        JSONObject jsonObject = JSON.parseObject(response).getJSONObject("data");
                        final String title = jsonObject.getString("title");
                        final String image = jsonObject.getString("image");
                        final String tostar = jsonObject.getString("tostar");
                        final String director = jsonObject.getString("director");
                        final String introduction = jsonObject.getString("introduction");

                        Glide.with(getContext())
                                .load(image)
                                .apply(REQUEST_OPTIONS)
                                .into(iv_category);
                        tv_category_title.setText(title);
                        tv_category_tostar.setText("演员：" + tostar);
                        tv_category_director.setText("导演：" + director);
                        tv_category_introduction.setText("介绍：" + introduction);
//                                X id: 38,
//                                title: "红楼梦",
//                                image: "http://10.3.100.200/videos/covers/202104/55.jpg",
//                                tostar: "",
//                                director: "",
//                                introduction: "",
//                                X tag: "连续剧",
//                                X category_enname: ""
                    }

                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Toast.makeText(getContext(), "加载专辑" + categoryId + "Failure", Toast.LENGTH_LONG).show();
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Toast.makeText(getContext(), "加载专辑" + categoryId + "Error" + "("+code+")" + msg, Toast.LENGTH_LONG).show();
                    }
                })
                .build()
                .get();
    }
}
